﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ERPConnection
{
    public static class CERPFunction
    {
        /// <summary>
        /// 获取所有账套对应的数据库名称
        /// </summary>
        /// <returns></returns>
        private static List<string> getCompDataNames()
        {
            LinqDataConetext.ERPDataClassesDataContext myContext = new LinqDataConetext.ERPDataClassesDataContext(PublicState.sunSystemConnection);
            List<string> compList = new List<string>();
            var s = from c in myContext.Comp select c;
            if (s.Count() > 0)
            {
                foreach (var l in s)
                {
                    compList.Add(l.compno);
                }
            }
            return compList;
        }
        /// <summary>
        /// 获取所有账套对应的数据库名称
        /// </summary>
        /// <returns></returns>
        public  static Dictionary<string,string> getCompDataNamesAndCpno()
        {
            LinqDataConetext.ERPDataClassesDataContext myContext = new LinqDataConetext.ERPDataClassesDataContext(PublicState.sunSystemConnection);
            Dictionary<string, string> compList = new Dictionary<string, string>();
            var s = from c in myContext.Comp select c;
            if (s.Count() > 0)
            {
                foreach (var l in s)
                {
                    compList.Add(l.compno,l.compname);
                }
            }
            return compList;
        }
        private static string getCompConnection(string compName)
        {
            return "data source=" + PublicState.dbSource + ";initial catalog=DB_" + compName + ";user id=" + PublicState.dbUserID + ";password=" + PublicState.dbPass;
        }
        /// <summary>
        /// 根据货品品号获取货品的相关信息
        /// </summary>
        /// <param name="prdno">货品品号</param>
        /// <returns>Dictionary  包含品号，品名，规格</returns>
        public static Dictionary<string,string> getPrdInfoByPrdNo(string prdno)
        {
            Dictionary<string, string> pinfo = new Dictionary<string, string>();
            List<string> compList = getCompDataNames();
            foreach(string comp in compList)
            {
                LinqDataConetext.ERPDataClassesDataContext myContext = new LinqDataConetext.ERPDataClassesDataContext(getCompConnection(comp));
                var prdinfo = from c in myContext.Prdt where c.PrdNo == prdno select c;
                if(prdinfo.Count()>0)
                {
                    pinfo.Add("prdno", prdno);
                    pinfo.Add("prdname", prdinfo.First().PrdName);
                    pinfo.Add("prdspc", prdinfo.First().PrdSpc);
                    break;
                }

            }
            return pinfo;
        }
        /// <summary>
        /// 根据货品品号检查货品在ERP哪个帐套中存在相关的制程信息
        /// </summary>
        /// <param name="prdno">货品品号</param>
        /// <returns>所有工序号的列表</returns>
        public static Dictionary<string,Common.CTFZC> checkTZWhereCompByPrdNo(string prdno)
        {
            string bomno=prdno+"->";
            Dictionary<string, Common.CTFZC> zcnoList = new Dictionary<string, Common.CTFZC>();
            List<string> compList = getCompDataNames();
            foreach (string comp in compList)
            {
                LinqDataConetext.ERPDataClassesDataContext myContext = new LinqDataConetext.ERPDataClassesDataContext(getCompConnection(comp));
                var prdinfo = from c in myContext.tfzc where c.bomno == bomno select c;
                if (prdinfo.Count() > 0)
                {
                    foreach(var s in prdinfo)
                    {
                        string zcno = s.zcno;
                        Common.CTFZC myzc = new Common.CTFZC();
                        myzc.zcno = s.zcno;
                        myzc.zcname = s.zcname;
                        myzc.zcrem = s.zcrem;
                        if(zcnoList.ContainsKey(zcno))
                        {
                            zcnoList.Add(zcno,myzc);
                        }
                    }
                }

            }
            return zcnoList;
        }
        /// <summary>
        /// 检查货品是否在ERP中存在
        /// </summary>
        /// <param name="prdNo">货品品号</param>
        /// <returns>True 存在，False 不存在</returns>
        public static bool checkPrdNo(string prdNo)
        {
            List<string> compList = getCompDataNames();
            foreach (string comp in compList)
            {
                LinqDataConetext.ERPDataClassesDataContext myContext = new LinqDataConetext.ERPDataClassesDataContext(getCompConnection(comp));
                var prdinfo = from c in myContext.Prdt where c.PrdNo == prdNo select c;
                if (prdinfo.Count() > 0)
                {
                    return true;
                }

            }
            return false;
        }
        /// <summary>
        ///  检查货品是否在ERP中存在
        /// </summary>
        /// <param name="prdNo">货品品号</param>
        /// <param name="compNo">帐套号</param>
        /// <returns>True 存在，False 不存在</returns>
        public static bool checkPrdNo(string prdNo,string compNo)
        {
            LinqDataConetext.ERPDataClassesDataContext myContext = new LinqDataConetext.ERPDataClassesDataContext(getCompConnection(compNo));
            var prdinfo = from c in myContext.Prdt where c.PrdNo == prdNo select c;
            if (prdinfo.Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 根据货品品号和帐套号获取对应产品的相关信息
        /// </summary>
        /// <param name="pno">品号</param>
        /// <param name="compno">帐套号</param>
        /// <returns></returns>
        public static Common.PrdInfo getPrdInfoByPrdNo(string pno,string compno)
        {
            Common.PrdInfo mypinfo = null;
            try
            {
                LinqDataConetext.ERPDataClassesDataContext myContext = new LinqDataConetext.ERPDataClassesDataContext(getCompConnection(compno));
                var pinfo = from c in myContext.Prdt where c.PrdNo == pno select c;
                if(pinfo.Count()>0)
                {
                    mypinfo = new Common.PrdInfo();
                    mypinfo.prd_no = pno;
                    mypinfo.prd_name = pinfo.First().PrdName;
                    mypinfo.prd_spc = pinfo.First().PrdSpc;
                    mypinfo.ut = pinfo.First().ut;
                    mypinfo.ut1 = pinfo.First().ut1;
                    mypinfo.knd = pinfo.First().knd;
                    mypinfo.chk = pinfo.First().chk_bat;
                    mypinfo.def_wh = pinfo.First().wh;
                    mypinfo.qo_ut = pinfo.First().dfu_ut;
                    mypinfo.jl_ut = pinfo.First().quote_ut2;
                }
            }
            catch
            {

            }
            return mypinfo;
        }
        /// <summary>
        /// 根据货品品号和帐套号获取下层BOM
        /// </summary>
        /// <param name="pno">货品品号</param>
        /// <param name="compno">帐套号</param>
        /// <returns></returns>
        public static List<Common.CTFBOM> getTFBOMList(string pno,string compno)
        {
            List<Common.CTFBOM> tfBOMList = new List<Common.CTFBOM>();
            LinqDataConetext.ERPDataClassesDataContext myContext = new LinqDataConetext.ERPDataClassesDataContext(getCompConnection(compno));
            var tfbom = from c in myContext.tfBOM where c.bomno == pno + "->" select c;
            if(tfbom.Count()>0)
            {
                foreach(var s in tfbom)
                {
                    Common.CTFBOM mybom = new Common.CTFBOM();
                    mybom.prdno = s.prdno;
                    mybom.pqty = s.qty;
                    tfBOMList.Add(mybom);
                }
            }
            return tfBOMList;
        }
    }
}
